package cn.shyouchi.wuguanjia.android.ui.meetting.common;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.meet.MeetApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.impl.OnItemClickListener;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.event.arrange.PersonRankAdapter;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SelMeetPersonActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, OnItemClickListener<MeetPerson> {


    public static final int REQ_CODE_HOLDER = 0;
    public static final int REQ_CODE_PERSON = 1;
    public static final int REQ_CODE_RECORDER = 2;
    public static final int REQ_CODE_DEPARTMENT = 3;
    public static final int RES_CODE_FAILURE = 0;
    public static final int RES_CODE_SUCCESS = 1;

    SelMeetPersonAdapter mSelMeetPersonAdapter;


    List<MeetPerson> mMeetPersons = new ArrayList<>();
    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.ck_sel_all)
    CheckBox mCkSelAll;
    @BindView(R.id.rv_persons)
    RecyclerView mRvPersons;

    MeetApi mMeetApi;

    boolean singleChoice;

    boolean selDepartment;


    MenuItem mSubMenuItem;

    @Override
    public int initContentView() {
        return R.layout.activity_sel_meet_person;
    }

    @Override
    public void initInjector() {
        mMeetApi = new MeetApi(mContext);
        singleChoice = getIntent().getBooleanExtra("single", false);
        selDepartment = getIntent().getBooleanExtra("selDepartment", false);

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        if (singleChoice) {
            mCkSelAll.setVisibility(View.GONE);
        }

        mCkSelAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mCkSelAll.isChecked()) {
                    selAll();
                } else {
                    unSelAll();
                }
                mSelMeetPersonAdapter.notifyDataSetChanged();
            }
        });

//        for (int i = 0; i < 20; i++) {
//            mMeetPersons.add(new MeetPerson(i, "Eenie" + i));
//        }


        mSelMeetPersonAdapter = new SelMeetPersonAdapter(mMeetPersons, this);
        mRvPersons.setAdapter(mSelMeetPersonAdapter);
        mRvPersons.setLayoutManager(new LinearLayoutManager(mContext));

//        fetchCrew();

        if (selDepartment) {
            fetchDepartment();
        } else {
            fetchCrew();
        }




    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Override
    public void onCheckedChanged(CompoundButton view, boolean isChecked) {
        if (isChecked) {
            selAll();
        } else {
            unSelAll();
        }
        mSelMeetPersonAdapter.notifyDataSetChanged();
    }

    private void selAll() {
        for (MeetPerson person : mMeetPersons) {
            person.setCheck(true);
        }
    }

    private void unSelAll() {
        for (MeetPerson person : mMeetPersons) {
            person.setCheck(false);
        }
    }


    private void fetchCrew() {
        mMeetApi.fetchCrew()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<List<MeetPerson>>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<List<MeetPerson>> baseReq) {
                        mMeetPersons.clear();
                        mMeetPersons.addAll(baseReq.getResultMessage());
                        mSelMeetPersonAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void fetchDepartment() {
        mMeetApi.fetchDepartment()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<List<MeetPerson>>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<List<MeetPerson>> baseReq) {
                        mMeetPersons.clear();
                        mMeetPersons.addAll(baseReq.getResultMessage());
                        mSelMeetPersonAdapter.notifyDataSetChanged();
                    }
                });
    }


    @Override
    public void onItemClick(MeetPerson entity) {
        if (singleChoice) {
            getIntent().putExtra("data", entity);
            setResult(RES_CODE_SUCCESS, getIntent());
            finish();
        }
    }

    @Override
    public void onDel(PersonRankAdapter.Person person) {
        if (person.getUser_id() == 0) {
            mCkSelAll.setChecked(true);
        } else {
            mCkSelAll.setChecked(false);
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!singleChoice) {
            mSubMenuItem = menu.add("确定");
            mSubMenuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        }
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mSubMenuItem == item) {
            getIntent().putParcelableArrayListExtra("data", mSelMeetPersonAdapter.getSelPerson());
            setResult(RES_CODE_SUCCESS, getIntent());
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
